package cn.sczhyt.modules.szhy.service.impl;

import cn.sczhyt.common.exception.RRException;
import cn.sczhyt.common.utils.*;
import cn.sczhyt.modules.szhy.entity.MeetingFileEntity;
import com.google.gson.JsonArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.sczhyt.modules.szhy.dao.MeetingLeaveDao;
import cn.sczhyt.modules.szhy.entity.MeetingLeaveEntity;
import cn.sczhyt.modules.szhy.service.MeetingLeaveService;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author Admin
 */
@Service("meetingLeaveService")
public class MeetingLeaveServiceImpl extends ServiceImpl<MeetingLeaveDao, MeetingLeaveEntity> implements MeetingLeaveService {

    /**
     * 文件配置
     */
    @Value("${cn.sczhyt.szhy.leave.upload-path}")
    private  String imagePath;
    /**
     * 文件配置
     */
    @Value("${cn.sczhyt.szhy.leave.server-path}")
    private  String imageServerPath;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MeetingLeaveEntity> page = this.page(
                new Query<MeetingLeaveEntity>().getPage(params),
                new QueryWrapper<MeetingLeaveEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R saveImages(MultipartFile[] files) {

        if (files != null && files.length > 0) {
            //根据时间创建文件夹
            String dateFile = DateUtils.format(new Date(), "yyyyMMdd");
            //创建文件名
            String filename = UUIDUtils.getUUID();
            //会议文件存储的路径

            String realPath = imagePath.concat(dateFile);

            File path = new File(realPath);
            if (!path.exists()) {
                path.mkdirs();
            }

            JSONObject object = new JSONObject();

            //保存文件
            if (files.length !=0) {
                for (int i = 0; i < files.length; i++) {

                        try {
                            String originalFilename = files[i].getOriginalFilename();
                            //文件存储位置
                            String filepath = realPath + "/" + originalFilename;

                            System.out.println(filepath);

                            files[i].transferTo(new File(filepath));
                            //修改权限
                            Runtime.getRuntime().exec("chmod 755 " + imagePath + dateFile);
                            Runtime.getRuntime().exec("chmod 755 " + filepath);

                            //上传成功后对路径及文件名进行处理
                            object.put("index", i + 1);
                            object.put("name", originalFilename);
                            object.put("url", imageServerPath + dateFile + "/" + originalFilename);

                        } catch (IOException e) {
                            e.printStackTrace();
                            throw new RRException("会议文件保存失败!", 201);
                        }
                    }
            }

            return R.ok().put("data",object);

    }
        return R.error(2005,"选择的文件为空");
    }
}